<?php 
class pager
{
	//代替  each
	function new_each(&$array){
	   $res = array();
	   $key = key($array);
	   if($key !== null){
		   next($array); 
		   $res[1] = $res['value'] = $array[$key];
		   $res[0] = $res['key'] = $key;
	   }else{
		   $res = false;
	   }
	   return $res;
	}
	
	
   /**
     * 系统后台的分页
     * 不需要传条件分页  
     * 后添加前台翻页函数
     * @param int    $pagecount 多少页
     * @param int    $page   当前第几页
     * @param string $table_style
     * @param string $font_style
     * @param int    $result_num       总条数
     * @param int    $page_size         该页显示的数量
     */
    function outputadmin($pagecount, $page, $table_style, $font_style, $result_num, $page_size){
        @$action        = $_SERVER['REDIRECT_URL'];
        $pagetable     = "";
        $pagecountlist = "";
        $temp          = "";                                       
        if(isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])){
        $query = explode("&",$_SERVER['QUERY_STRING']);
            while(list($index, $value) = $this->new_each($query)) {
                $a = explode("=",$value);
                if(strcmp(strtolower($a[0]),"page")!=0){
                    $temp .= $a[0]."=".$a[1]."&";
                }
            }
        }else{
            $temp = "";
        }
        $pagetable .=  "<div class=\"page_count\" align=\"left\">\n";
        if($pagecount > 1)
        {
            $pagetable .="<label>共有: " . $result_num . " 条/ ".$pagecount." 页 </label>";
            if($page<=1){
                $pagetable .="<a href='#this' class=\"paginate_button\">首页</a>&nbsp;";
                $pagetable .= "&nbsp;<a href='#this' class=\"paginate_button\">上一页</a>&nbsp;";
            }
            else{
                $pagetable .= "<a class=\"paginate_button\" href=" . $action . "?" . $temp . "page=1>首页</a>&nbsp;";
                $pagetable .= "&nbsp;<a class=\"paginate_button\" href=" . $action . "?" . $temp . "page=" . ($page-1) . ">上一页</a>&nbsp;";
            }
            $start = 1;
            for($i=$start;$i<=$pagecount;$i++){
                $k=$page+5;
                $f=$page-4;
                if($page<=5 && $i<=10){
                    if($page == $i){
                        $pagecountlist .= "&nbsp;<span class=\"paginate_active\">".$i."</span>&nbsp;";
                    }else{
                        $pagecountlist .= "&nbsp;<a class=\"paginate_button\" href=" . $action . "?" . $temp . "page=" . $i . ">".$i."</a>&nbsp;";
                    }
                }elseif($k>=$pagecount && $i>($pagecount-10)){
                    if($page == $i){
                        $pagecountlist .= "&nbsp;<span class=\"paginate_active\">".$i."</span>&nbsp;";
                    }else{
                        $pagecountlist .= "&nbsp;<a class=\"paginate_button\" href=" . $action . "?" . $temp . "page=" . $i . ">".$i."</a>&nbsp;";
                    }
                }elseif($i>=$f && $i<=$k){
                    if($page == $i){
                        $pagecountlist .= "&nbsp;<span class=\"paginate_active\">".$i."</span>&nbsp;";
                    }else{
                        $pagecountlist .= "&nbsp;<a class=\"paginate_button\" href=" . $action . "?" . $temp . "page=" . $i . ">".$i."</a>&nbsp;";
                    }
                }
            }
        }
        $pagetable .= $pagecountlist."";
        if($pagecount > 1)
        {
            if($page>=$pagecount){
            $pagetable .= "&nbsp;<a href='#this' class=\"next paginate_button paginate_button_disabled\">下一页</a>&nbsp;";
            $pagetable .= "&nbsp;<a href='#this' class=\"last paginate_button paginate_button_disabled\">尾页</a>&nbsp;";
            }
            else{
            $pagetable .= "&nbsp;<a class=\"next paginate_button paginate_button_disabled\" href=" . $action . "?" . $temp . "page=" . ($page+1) . ">下一页</a>&nbsp;";
            $pagetable .= "&nbsp;<a class=\"last paginate_button paginate_button_disabled\" href=" . $action . "?" . $temp . "page=" . $pagecount . ">尾页</a>&nbsp;";
            }
        }
        $pagetable .=  "<div style=\"clear: both;\"></div></div>";
        return $pagetable;
    }
   
   
   
   
   
   
    /**
     * 不需要传条件分页  
     * 后添加前台翻页函数
     * @param int    $pagecount 多少页
     * @param int    $page   当前第几页
     * @param string $table_style
     * @param string $font_style
     * @param int    $result_num       总条数
     * @param int    $page_size         该页显示的数量
     */
    function output($pagecount, $page, $table_style, $font_style, $result_num, $page_size){
       
                                   
        @$action        = $_SERVER['REDIRECT_URL'];
       
        $pagetable     = "";
        $pagecountlist = "";
        $temp          = "";                                       
        if(isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])){
        $query = explode("&",$_SERVER['QUERY_STRING']);
            while(list($index, $value) = each($query)) {
                $a = explode("=",$value);
                if(strcmp(strtolower($a[0]),"page")!=0){
                    $temp .= $a[0]."=".$a[1]."&";
                }
            }
        }else{
            $temp = "";
        }      
        $pagetable .=  "<div class=\"page\" align=\"left\">\n";
        if($pagecount > 1)
        {
                $pagetable .="共有: " . $result_num . " 条 ";   
                if($page<=1){
                    //$pagetable .="<a href='#this' class=\"fy\">首页</a>&nbsp;";
                    $pagetable .= "&nbsp;<a href='#this' class=\"fy\">上一页</a>&nbsp;";
                }
                else{
                   // $pagetable .= "<a class=\"fy\" href=" . $action . "?" . $temp . "page=1>首页</a>&nbsp;";
                    $pagetable .= "&nbsp;<a class=\"fy\" href=" . $action . "?" . $temp . "page=" . ($page-1) . ">上一页</a>&nbsp;";
                }
 
            
                $start = (ceil($page/10)-1)*8+1;       
                $end   = ceil($page/10)*8;
          
                if($start <= 0) $start = 1;
                if($end   >= $pagecount) $end = $pagecount;
                for($i=$start;$i<=$end;$i++)
                {
                    if($page == $i)
                        $pagecountlist .= "&nbsp;<span class='dy'>".$i."</span>&nbsp;";
                    else
                        $pagecountlist .= "&nbsp;<a class=\"fy\" href=" . $action . "?" . $temp . "page=" . $i . ">".$i."</a>&nbsp;";
                }
                
                
        }

        $pagetable .= $pagecountlist."";

        
    if($pagecount > 1)
        {
            if($page>=$pagecount){
            $pagetable .= "&nbsp;<a href='#this' class=\"fy\">下一页</a>&nbsp;";
           // $pagetable .= "&nbsp;<a href='#this' class=\"fy\">尾页</a>&nbsp;";
            }
            else{
            $pagetable .= "&nbsp;<a class=\"fy\" href=" . $action . "?" . $temp . "page=" . ($page+1) . ">下一页</a>&nbsp;";
          //  $pagetable .= "&nbsp;<a class=\"fy\" href=" . $action . "?" . $temp . "page=" . $pagecount . ">尾页</a>&nbsp;";
            }
        }

        $pagetable .=  "</div>";
        
        return $pagetable;
    }
    
   
    /*留言分页*/
    function ajaxpage($pagecount, $page, $table_style, $font_style, $result_num, $page_size,$id=''){
       
                                   
        @$action        = $_SERVER['REDIRECT_URL'];
       
        $pagetable     = "";
        $pagecountlist = "";
        $temp          = "";                                       
        if(isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])){
        $query = explode("&",$_SERVER['QUERY_STRING']);
            while(list($index, $value) = each($query)) {
                $a = explode("=",$value);
                if(strcmp(strtolower($a[0]),"page")!=0){
                    $temp .= $a[0]."=".$a[1]."&";
                }
            }
        }else{
            $temp = "";
        }      
        $pagetable .=  "<p class=\"pages\">\n";
        if($pagecount > 1)
        {
               //$pagetable .="<span>共有" . $result_num . "条记录</span>";   
                if($page<=1){
                    $pagetable .="&nbsp;<a href='#this' class=\"pg_home not\">首页</a>&nbsp;";
                    $pagetable .= "&nbsp;<a href='#this' class=\"pre_page not\">上一页</a>&nbsp;";
                }
                else{
                    $pagetable .= "<a class=\"last_page\" href=\"javascript:void(0);\"onclick='page(1,$id)'>首页</a>&nbsp;";
                    $pagetable .= "&nbsp;<a class=\"next_page\" href=\"javascript:void(0);\"onclick='page(".($page-1).",$id)'>上一页</a>&nbsp;";
                }
 
            
                $start = (ceil($page/10)-1)*10+1;       
                $end   = ceil($page/10)*10;
          
                if($start <= 0) $start = 1;
                if($end   >= $pagecount) $end = $pagecount;
                for($i=$start;$i<=$end;$i++)
                {
                    if($page == $i)
                        $pagecountlist .= '<a class="list_fenye_on" href="#">'.$i.'</a>';
                    else
                        $pagecountlist .= "&nbsp;<a href=\"javascript:void(0);\" onclick='page(".$i.",$id)'>".$i."</a>&nbsp;";
                }
                
                
        }

        $pagetable .= $pagecountlist."";

        
    if($pagecount > 1)
        {
            if($page>=$pagecount){
            $pagetable .= "<a href='#this' class=\"pre_page not\">下一页</a>";
            //$pagetable .= "&nbsp;<a href='#this' class=\"pg_home not\">尾页</a>&nbsp;";
            }
            else{
            $pagetable .= "&nbsp;<a class=\"next_page\" href=\"javascript:void(0);\"onclick='page(".($page+1).",$id)'>下一页</a>&nbsp;";
            $pagetable .= "&nbsp;<a class=\"last_page\" href=\"javascript:void(0);\"onclick='page(".$pagecount.",$id)'>尾页</a>&nbsp;";
            }
        }
       // $pagetable .=  "<span>共有".$pagecount."页</span>";
        //$pagetable .=  "<span class=\"directto\">直达<input type=\"text\" id=\"directtopage\" />页</span>";
        $pagetable .=  "</p>";
        if($result_num!=0)return $pagetable; 
    }





    /**
     * 
     * 评论的分页
     * @param int $pagetotal 分页总数
     * @param int $curr 当前页
     */
    public function collectpage($pagearr) {
	    if (preg_match('/\?page=\w+/i',$_SERVER['REQUEST_URI']) > 0){
	    	$url_format = preg_replace('/\?page=\w+/i', '?', $_SERVER['REQUEST_URI']);
		} elseif(preg_match('/\&page=\w+/i',$_SERVER['REQUEST_URI']) > 0) {
			$url_format = preg_replace('/\&page=\w+/i', '&', $_SERVER['REQUEST_URI']);
		}else{
			if(strpos($_SERVER['REQUEST_URI'],'?') !== false) {
				$url_format = $_SERVER['REQUEST_URI'] . '&';
			} else {
				$url_format = $_SERVER['REQUEST_URI'] . '?';
			}
		}
	    $segment	= 3;
		$max_pages	= 6;
		$start		= ($pagearr['currpage'] <= $segment) ? 1 : $pagearr['currpage'] - $segment;
		$end		= ($pagearr['pagetotal'] <= $start + $max_pages - 1) ? $pagearr['pagetotal'] : $start + $max_pages - 1;
		if ($end == $pagearr['pagetotal'] && $pagearr['pagetotal'] >= $max_pages){
			$start = $end - $max_pages + 1;
		}
		$out		= "";
		$prve_page	= $pagearr['currpage'] - 1;
		$next_page	= $pagearr['currpage'] + 1;
		if ($pagearr['pagetotal'] > 1){
			if ($start > 1){
				$out .= '<a href="'.$url_format.'page='.($pagearr['currpage']-1).'">← 上一页</a>';
			}
			for ($i = $start; $i <= $end; $i++){
				if($end == $i) {
					$out .= ($i != $pagearr['currpage']) ?
						'<a href="'.$url_format.'page='.$i.'" >'.$i.'</a>&nbsp;' :
						'&nbsp;<a href="#this" class="cur">'.$i.'</a>&nbsp;' ;
				} else {
					$out .= ($i != $pagearr['currpage']) ?
						'<a href="'.$url_format.'page='.$i.'" >'.$i.'</a>&nbsp;' :
						'&nbsp;<a href="#this" class="cur">'.$i.'</a>&nbsp;' ;
				}
			}
			if($pagearr['currpage'] < $pagearr['pagetotal']) {
				$out .= '&nbsp;<a href="'.$url_format.'page='.($pagearr['currpage'] + 1).'">下一页 →</a>';
			}
		}
		return $out;
    }
    
 	public function resourcepage($pagearr) {
 		if(empty($pagearr) || empty($pagearr['currenturl']) || empty($pagearr['pagetotal']) || empty($pagearr['currpage'])) return '';
	    $url_format = $pagearr['currenturl'];
	    $segment	= 4;
		$max_pages	= 9;
		$start		= ($pagearr['currpage'] <= $segment) ? 1 : $pagearr['currpage'] - $segment;
		$end		= ($pagearr['pagetotal'] <= $start + $max_pages - 1) ? $pagearr['pagetotal'] : $start + $max_pages - 1;
		if ($end == $pagearr['pagetotal'] && $pagearr['pagetotal'] >= $max_pages){
			$start = $end - $max_pages + 1;
		}
		$out		= "";
		$prve_page	= $pagearr['currpage'] - 1;
		$next_page	= $pagearr['currpage'] + 1;
		if ($pagearr['pagetotal'] > 1){
			if ($start > 1){
				$out .= '<li class="netPage"><a href="'.$url_format.'/page/'.($pagearr['currpage']-1).'.html">← 上一页</a></li>';
			}
			for ($i = $start; $i <= $end; $i++){
				if($end == $i) {
					$out .= ($i != $pagearr['currpage']) ?
						'<li><a href="'.$url_format.'/page/'.$i.'.html" >'.$i.'</a></li>' :
						'<li class="clik"><a href="#this">'.$i.'</a></li>' ;
				} else {
					$out .= ($i != $pagearr['currpage']) ?
						'<li><a href="'.$url_format.'/page/'.$i.'.html" >'.$i.'</a></li>' :
						'<li class="clik"><a href="#this">'.$i.'</a></li>' ;
				}
			}
			if($pagearr['currpage'] < $pagearr['pagetotal']) {
				$out .= '<li class="Pagedon"><a href="'.$url_format.'/page/'.($pagearr['currpage'] + 1).'.html">下一页 →</a></li>';
			}
		}
		return $out;
    }
    
}